﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Linq;

namespace MWTWorkUnitMgmtTool.DataAccess.TableGatewayRepository
{
    public class TableGatewayRepository:ITableGatewayRepository
    {
        public TableGatewayRepository()
        {
            
        }

        #region ITableGatewayRepository Members

        public IEnumerable<System.Data.IDataRecord> Retrieve(string sql, string connectionString, Action<System.Data.SqlClient.SqlParameterCollection> addParameters)
        {
            using(var connection = new SqlConnection(connectionString))
            using (var command = new SqlCommand(sql, connection))
            {
                addParameters(command.Parameters);
                connection.Open();

                using (var rdr = command.ExecuteReader())
                {
                    while (rdr.Read())
                        yield return rdr;

                    rdr.Close(); 
                }
            }
        }

        public int Commit(string sql, string connectionString, Action<SqlParameterCollection> addParameters)
        {
            using(var connection = new SqlConnection(connectionString))
            using (var command = new SqlCommand(sql, connection))
            {
                addParameters(command.Parameters);
                connection.Open();
                int i = command.ExecuteNonQuery();
                return i;
            }
        }

        #endregion

        
    }
}
